package bairui.international.service.impl;

import java.util.List;

import org.springframework.data.querydsl.QSort;
import org.springframework.stereotype.Service;

import com.lianjing.sh.validate.spring.exception.ValidateObjectException;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Predicate;

import bairui.international.model.domain.jpa.TB_ProductCategory;
import bairui.international.service.Service_ProductCategory;

/**
 * 
 * 【(物料分类)TB_ProductCategory】服务实现
 *
 * @generated
 * 
 * @see bairui.model.domain.jpa.TB_ProductCategory 模型
 * @see Service_ProductCategory 服务接口
 * @see BService_ProductCategoryImpl 服务实现基础
 *
 */
@Service
public class Service_ProductCategoryImpl extends BService_ProductCategoryImpl implements Service_ProductCategory {

	/**
	 * 查找根分类
	 */
	@Override
	public TB_ProductCategory findRoot(TB_ProductCategory category){
		TB_ProductCategory parent = load(category).getParent();
		if(null == parent){
			return category;
		}
		return findRoot(parent);
	}

	@Override
	public List<TB_ProductCategory> findSubProductCategory(Long parentId, OrderSpecifier<?>... orders) {
		return list(it->it.parent.id.eq(parentId), it->new QSort(orders)).getContent();
	}

	@Override
	public List<TB_ProductCategory> findRootProductCategory(Predicate query, OrderSpecifier<?>... orders) {
		return list(it->it.parent.isNull().and(query), it->new QSort(orders)).getContent();
	}
	
	@Override
	public TB_ProductCategory save(TB_ProductCategory bean, Long parentId) throws ValidateObjectException {
		// 验证
		if(null == parentId){
			if(exists(
				it->it.number.eq(bean.getNumber()).and(it.parent.isNull())
			)){
				throw new ValidateObjectException("object", "", "编号已经存在");
			}
		}else{
			if(exists(
				it->it.number.eq(bean.getNumber()).and(it.parent.id.eq(parentId))
			)){
				throw new ValidateObjectException("object", "", "编号已经存在");
			}
		}
		// 添加父节点
		if(null != parentId){
			bean.setParent(new TB_ProductCategory(parentId));
		}
		//
		TB_ProductCategory inputParent = bean.getParent();
		if(null == inputParent){
			bean.setLevel(0);
			bean.setRoot(bean);
		}else{
			TB_ProductCategory findParent = dao.findById(inputParent.getId()).get();
			// root
			TB_ProductCategory parentParent = findParent.getParent();
			if (null == parentParent){
				bean.setRoot(findParent);
			}else{
				bean.setRoot(findParent.getRoot());
			}
			// level
			bean.setLevel(findParent.getLevel()+1);
		}
		return super.save(bean);
	}

	@Override
	public void update(long id, TB_ProductCategory input) throws Exception {
		TB_ProductCategory bean = findById(id);
		TB_ProductCategory parent = bean.getParent();
		// 验证
		if(!bean.getNumber().equals(input.getNumber())){
			if(null == parent){
				if(exists(
					it->it.number.eq(input.getNumber()).and(it.parent.isNull())
				)){
					throw new ValidateObjectException("object", "", "编号已经存在");
				}
			}else{
				if(exists(
					it->it.number.eq(input.getNumber()).and(it.parent.eq(parent))
				)){
					throw new ValidateObjectException("object", "", "编号已经存在");
				}
			}
		}
		//
		updateWithNonNullProperties(id, input);		
	}
	
}
